package com.lehecai.ucenter.action.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.lehecai.core.YesNoStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.lehecai.ucenter.action.BaseAction;
import com.lehecai.ucenter.dao.QueryParam;
import com.lehecai.ucenter.entity.app.App;
import com.lehecai.ucenter.generator.AppTokenSecretGenerator;
import com.lehecai.ucenter.service.app.AppService;
import com.lehecai.ucenter.utils.PageUtil;

public class AppAction extends BaseAction {
	private static final long serialVersionUID = -8830679912602886965L;
	
	private AppService appService;
	
	private App app;
	private List<App> apps;
	private List<YesNoStatus> yesNoStatusList;
	private Integer otpFlagValue;
	
	public String handle() {
		HttpServletRequest request = ServletActionContext.getRequest();
		QueryParam queryParam = new QueryParam();
		queryParam.setPageBean(super.getPageBean());
		apps = appService.findList(queryParam);
		super.setPageString(PageUtil.getPageString(request, queryParam.getPageBean()));
		return "list";
	}
	
	public String input() {
		if (app != null && app.getId() != null) {
			app = appService.get(app.getId());
			otpFlagValue = app.getOtpFlag().getValue();
		} else {
			app = new App();
			otpFlagValue = YesNoStatus.NO.getValue();
		}
		return "inputForm";
	}
	
	public String manage() {
		if (app != null) {
			if (StringUtils.isEmpty(app.getName())) {
				logger.error("添加子系统，名称为空");
				super.setErrorMessage("添加子系统名称不能为空");
				return "inputForm";
			}
			if (app.getId() == null) {
				app.setToken(AppTokenSecretGenerator.generateAppToken());
				app.setSecret(AppTokenSecretGenerator.generateAppSecret());
				app.setOtpFlag(YesNoStatus.getItem(otpFlagValue));
				appService.update(app);
			} else {
				App sApp = appService.get(app.getId());
				if (sApp == null || sApp.getId() == null) {
					logger.error("子系统不存在");
					super.setErrorMessage("子系统不存在");
					return "failure";
				}
				sApp.setName(app.getName());
				sApp.setUrl(app.getUrl());
				sApp.setOtpFlag(YesNoStatus.getItem(otpFlagValue));
				appService.update(sApp);
			}
		} else {
			logger.error("添加子系统不错误，提交表单为空");
			super.setErrorMessage("添加子系统不错误，提交表单不能为空");
			return "failure";
		}
		super.setForwardUrl("/app/app.do");
		return "success";
	}
	
	public String view() {
		logger.info("进入查看子系统详情");
		if (app != null && app.getId() != null) {
			app = appService.get(app.getId());
		} else {
			logger.error("查看子系统详情，编码为空");
			super.setErrorMessage("查看子系统详情，编码为空");
			return "failure";
		}
		logger.info("查看子系统详情结束");
		return "view";
	}
	
	public String del() {
		logger.info("进入删除子系统");
		if (app != null && app.getId() != null) {
			app = appService.getFormDb(app.getId());
			appService.delete(app);
		} else {
			logger.error("删除子系统，编码为空");
			super.setErrorMessage("删除子系统，编码不能为空");
			return "failure";
		}
		super.setForwardUrl("/app/app.do");
		logger.info("删除子系统结束");
		return "forward";
	}
	
	public String updateSecret() {
		if (app != null && app.getId() != null) {
			app = appService.get(app.getId());
			app.setSecret(AppTokenSecretGenerator.generateAppSecret());
			appService.update(app);
		} else {
			logger.error("更新secret，编码为空");
			super.setErrorMessage("更新secret，编码不能为空");
			return "failure";
		}
		super.setForwardUrl("/app/app.do");
		return "forward";
	}

	public App getApp() {
		return app;
	}

	public void setApp(App app) {
		this.app = app;
	}

	public List<App> getApps() {
		return apps;
	}

	public void setApps(List<App> apps) {
		this.apps = apps;
	}

	public AppService getAppService() {
		return appService;
	}

	public void setAppService(AppService appService) {
		this.appService = appService;
	}

	public List<YesNoStatus> getYesNoStatusList() {
		return YesNoStatus.getItems();
	}

	public void setYesNoStatusList(List<YesNoStatus> yesNoStatusList) {
		this.yesNoStatusList = yesNoStatusList;
	}

	public Integer getOtpFlagValue() {
		return otpFlagValue;
	}

	public void setOtpFlagValue(Integer otpFlagValue) {
		this.otpFlagValue = otpFlagValue;
	}
}
